<?php
/**
 * 共通オブジェクト指向データベース操作クラス
 * @author rhh
 */
class Common_model extends CI_Model {


	function __construct()
	{
		parent::__construct();
		//parent::Model();
	}

	function standard_insert($sql)
	{

// 		$sql = "INSERT INTO mytable (title, name)
//         VALUES (".$this->db->escape($title).", ".$this->db->escape($name).")";

		$this->db->query($sql);

		//echo $this->db->affected_rows();

	}

	function common_update($tableNm, $data, $where)
	{

		if ($this->db->update($tableNm, $data, $where)) {

			return 'up_success';
		}
		return 'up_fail';


	}

	function common_insert($tableNm, $data)
	{

		if ($this->db->insert($tableNm, $data)) {

			return 'ins_success';
		}
		return 'ins_fail';

	}

	function common_delete($tableNm, $data)
	{

		if ($this->db->delete($tableNm, $data)) {
			return 'del_success';
		}
		return 'del_fail';

	}

	function update_batch($tableNm, $data, $where)
	{

		if ($this->db->update_batch($tableNm, $data, $where)) {

			return 'up_success';
		}
		return 'up_fail';


	}
	//一括INSERT用
	function insert_batch($tableNm, $data)
	{
// 		$data = array(
// 				array(
// 						'first_name' => '一郎',
// 						'family_name' => '鈴木',
// 				),
// 				array(
// 						'first_name' => '太郎',
// 						'family_name' => '山田',
// 				),
// 		);
// 		$this->db->insert_batch($tableNm, $data);

		echo $tableNm;
		foreach($data as $row){
			print_r($row);//Array ( [1] => Array ( [1] => a [2] => b [3] => c [4] => d [5] => e [6] => g [7] => ggg [999] => pppp ) )
			//$result[]   =   str_replace('"', '',$row);
		}
		if ($this->db->insert_batch($tableNm, $data)) {

			return 'ins_bat_success';
		}
		return 'ins_bat_fail';

	}

	function delete_batch($tableNm, $data)
	{

		if ($this->db->delete_batch($tableNm, $data)) {
			return 'del_success';
		}
		return 'del_fail';

	}

	//key存在チェック
	function unique_check($tblNm, $whereArray) {
//echo $tblNm;
//print_r($whereArray);
		$this->db->from($tblNm);

		foreach ($whereArray as $key => $value) {
			$this->db->where($key, $value);
		}
		//where($key, $value = NULL, $escape = TRUE)
		//$this->db->where('family_name', '鈴木');
		$query = $this->db->get();
		if ($query->num_rows() > 0) {
		    return '1'; //true;
		}

		// 結果データがありません。
		return '0';


	}
	//クエリの結果からCSVを生成
	function export_csv($tblNm) {

		$delimiter = ",";
		$newline = "\r\n";

		//ユーティリティクラスの初期化
		$this->load->dbutil();

	//echo $tblNm;
		//ALLの場合、すべてのテーブルのALLデータをCSVに？？
		if ($tblNm == 'ALL') {

			$allTable = array();
			//$dbs = array("","","");//$this->dbutil->list_databases();
			foreach ($this->db->list_tables() as $table_name)
			{
				//echo $table_name;

					$query = $this->db->query("SELECT * FROM ".$table_name);

					//echo $this->dbutil->csv_from_result($query, $delimiter, $newline);
					$allTable[$table_name] = $this->dbutil->csv_from_result($query, $delimiter, $newline);

			}

			return $allTable;
		} else {
			$existsTable = TRUE;
// 			$existsTable = FALSE;
// 			foreach ($this->db->list_tables() as $table_name)
// 			{
// 				if ($table_name == $tblNm) {
// 					$existsTable = TRUE;
// 					break;
// 				}
// 			}

			if ($existsTable) {
	//echo "table_exists-".$tblNm;
				$query = $this->db->query("SELECT * FROM ".$tblNm);
				return  $this->dbutil->csv_from_result($query, $delimiter, $newline);
			}




	//echo "not table_exists-".$existsTable;

			return '';

		}
	}


//------ページ用SELECT-----------//

	function getPagingData( $tbl, $where, $select = '*', $start = 1, $limit  = 10, $sidx = null, $sord = 'ASC', $join = null){

		$this->db->select($select);
// 		$this->db->limit($limit, $start);
		if($where != NULL) {
			if (is_array($where)) {
				foreach ($where as $key => $value) {

					if ($key == 'or_like') {
					//if (in_array('or_like', $key)) {

						$this->db->or_like($value);

					}else if ($key == 'like') {

						$this->db->like($value);

					} else {
						$this->db->where($key, $value);
					}
				}
			} else {

				$this->db->where($where, NULL, TRUE); //, NULL, TRUE);//key,val,esc
			}

		}

		if($join != NULL) {
			foreach ($join as $key => $value) {
				$this->db->join($key, $value, "left");
			}
		}

		if($sidx != NULL) {
			$this->db->order_by($sidx, $sord);
		}


		$query = $this->db->get($tbl, $limit, $start);
		//SELECT * FROM `$tbl` WHERE '$where' LIMIT 30($start), 10( $limit);

		return $query->result();
	}

	function getPagingDataPtwo( $sql,$param, $start = 1, $limit  = 10, $sidx = null, $sord = 'ASC'){
		//echo $sql.'---'.$param;
		if($sidx != NULL) {
			$this->db->order_by($sidx, $sord);
		}

		$sql.= ' limit '.$start.', '.$limit;
		$rs = $this->db->query($sql, $param);
		return $rs->result();
	}

	//ページ用_TOTAL_COUNT取得
	public function gets_totalPtwo($sql, $param) {
		//echo $sql.'---'.$param;
		$rs = $this->db->query($sql, $param);
		return $rs->num_rows();
	}


	public function gets($tbl_nm, $page, $rp, $where = '') {
		// オブジェクト

		$sql="select * from ".$tbl_nm ; //commodity_header ";
		$sql.= ' limit '.$page.', '.$rp;
		$rs = $this->db->query($sql);
		//return $rs->result_array();
		return $rs->result();

		// 		return $this->db->query("select * from test")->result_array();
	}

	//ページ用_TOTAL_COUNT取得
	public function gets_total($tbl_nm, $where) {

		if($where != NULL) {
			if (is_array($where)) {
				foreach ($where as $key => $value) {

					if ($key == 'or_like') {
						//if (in_array('or_like', $key)) {

						$this->db->or_like($value);

					}else if ($key == 'like') {

						$this->db->like($value);

					} else {
						$this->db->where($key, $value);
					}
				}
			} else {

				$this->db->where($where, NULL, TRUE); //, NULL, TRUE);//key,val,esc
			}
		}
		// オブジェクト
	//	$rs = $this->db->query("select * from ".$tbl_nm." where 1=1 ");
		$rs = $this->db->get($tbl_nm);

		return $rs->num_rows();
	}


	/**
	 * now
	 *
	 * @return string
	 */
	public function now() {
		return date('Y-m-d H:i:s');
	}

}
